package com.soxing.webrain.brush.controller;


import com.soxing.webrain.brush.api.IFeedBackService;
import com.soxing.webrain.brush.domain.view.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;

@RestController("/feedback")
@Api(value = "神经反馈", tags = {"神经反馈"})
@Slf4j
public class
FeedbackController {

    @Resource
    IFeedBackService feedBackService;

   // 懒得写出maven了 就这样先调试吧  oneValuePathNet
    @Value("${feedback.oneValuePathPower}")
    String oneValuePathPower;

    @Value("${feedback.oneValuePathNet}")
    String oneValuePathNet;

    @Value("${feedback.powerPath}")
    String powerPath;

    @Value("${feedback.sourcePath}")
    String sourcePath;

    @Value("${feedback.netPath}")
    String netPath;

//
//    String oneValuePath = "/home/skye/FB/results1";
//    String powerPath = "/home/skye/FB/results1";
//    String sourcePath = "/home/skye/FB/results1";
//
//    String netPath = "/home/skye/FB/results";


//    String oneValuePath = "/home/webrain/Desktop/feedback/power";
//    String powerPath = "/home/webrain/Desktop/feedback/power";
//    String sourcePath = "/home/webrain/Desktop/share/project/feedback/file/source";
//
//    String netPath = "/home/webrain/Desktop/feedback/net";


    @GetMapping("/oneValuePathPower")
    @ApiOperation(value = "单值反馈")
    public R getOneValuePower(){
        String result = feedBackService.getOneValue(oneValuePathPower);

        return R.ok().data("data",result).data("mode",1);
    }


    @GetMapping("/oneValuePathNet")
    @ApiOperation(value = "单值反馈")
    public R getOneValueNet(){
        String result = feedBackService.getOneValue(oneValuePathNet);

        return R.ok().data("data",result).data("mode",5);
    }

    @GetMapping("/get2dPowerValue")
    @ApiOperation(value = "2d功率谱反馈")
    public R get2dPowerValue(){
        R result = feedBackService.get2dPowerValue(powerPath);
        return result;
    }

    @GetMapping("/get3dSourceImage")
    @ApiOperation(value = "3d溯源结果反馈")
    public R get3dSourceImage(){
        R result = feedBackService.get3dSourceImage(sourcePath);
        return result;
    }


    @GetMapping("/get3dNetValue")
    @ApiOperation(value = "3d脑网络反馈")
    public R get3dNetValue(String resultsPath){
        R result = feedBackService.get3dNetValue(resultsPath + "/result_network");
        return result;

    }

    @GetMapping("/feedback")
    public R feedback(@RequestParam String resultsPath){
        //在pro模式下 source路径和功率谱一样了,以后要改一下的.
        //这样写有点笨重了,而且应该用枚举的.加个策略模式的,emmm,这样写也可以,但是不够优雅.
//        File oneValueFilePower = feedBackService.getLastModifiedFile(new File(oneValuePathPower),".ini");
//        File oneValueFileNet = feedBackService.getLastModifiedFile(new File(oneValuePathNet),".ini");
//        File powerFile = feedBackService.getLastModifiedFile(new File(powerPath),".mat");
//        File netPathFile = feedBackService.getLastModifiedFile(new File(netPath),".mat");
//        File sourceFile = feedBackService.getLastModifiedFile(new File(sourcePath),".mat");

        File oneValueFilePower = feedBackService.getLastModifiedFile(new File(resultsPath + "/result_power"),".ini");
        File oneValueFileNet = feedBackService.getLastModifiedFile(new File(resultsPath + "/result_network"),".ini");
        File powerFile = feedBackService.getLastModifiedFile(new File(resultsPath + "/result_power"),".mat");
        File netPathFile = feedBackService.getLastModifiedFile(new File(resultsPath + "/result_network"),".mat");
        File sourceFile = feedBackService.getLastModifiedFile(new File(resultsPath + "/result_source"),".mat");

        File now = oneValueFilePower;
        int mode = oneValueFilePower==null?-1:1;
        Boolean hasResultFlag = false;
        if(powerFile!=null&&(now==null||powerFile.lastModified()>now.lastModified())){
            now = powerFile;
            mode = 2;
            hasResultFlag = true;
        }
        if((netPathFile!=null)&&(now==null||netPathFile.lastModified()>now.lastModified())){
            now = netPathFile;
            mode = 3;
            hasResultFlag = true;
        }
        if(sourceFile!=null&&(now==null||sourceFile.lastModified()>now.lastModified())){
            now = sourceFile;
            mode = 4;
            hasResultFlag = true;
        }
        if(oneValueFileNet!=null&&(now==null||oneValueFileNet.lastModified()>now.lastModified())){
            now = sourceFile;
            mode = 5;
            hasResultFlag = true;
        }
        if (hasResultFlag == false) mode =0;

        //mode = 3;//测试用的

        log.info("访问的模式是:"+mode);
        if(mode==1){
            return getOneValuePower();
        }
        else if (mode == 2){
            return get2dPowerValue();
        }
        else if (mode == 3){
            return get3dNetValue(resultsPath);
        }
        else if (mode == 4){
            return get3dSourceImage();
        }
        else if (mode == 5){
            return getOneValueNet();
        }
        else if (mode == 0){
            return R.ok().data("oneValue",null).data("M_zscore_mean",null).data("chanlocs",null).data("mode",3).data("zoneLabel",null).data("zone_chan",null);
        } else {
            return R.error().data("mode",-1);
        }

    }



}
